#include "MessagesHandler.h"
#include "MessagesDefs.h"
#include "Message.h"
#include "MessageLogIn.h"
#include "MessageMove.h"
#include "MessageDoRocket.h"
#include "MessageDoubleLogIn.h"

#include "../DataBase/DataBaseConnection.h"
#include "../Server.h"



MessagesHandler* MessagesHandler::m_instance = NULL;
std::vector< Message* >	MessagesHandler::m_pendingMessages;

//====================================================================
//
//
//====================================================================
MessagesHandler::MessagesHandler(void)
{
}

//====================================================================
//
//
//====================================================================
MessagesHandler::~MessagesHandler(void)
{
}

//====================================================================
//
//
//====================================================================
MessagesHandler* MessagesHandler::getInstance()
{
	if(!m_instance)
	{
		m_instance = new MessagesHandler();
	}

	return m_instance;
}

//====================================================================
//
//
//====================================================================
void MessagesHandler::addMessage(Message* msg)
{
	m_pendingMessages.push_back(msg);
}

//====================================================================
//
//
//====================================================================
void MessagesHandler::createMessage(ByteArray* data, bool isReceived, Connection* connection)
{
	int type		= data->readInt();
	std::string id	= data->readUTF();
	Message* msg	= NULL;

	bool ok = true;

	switch(type)
	{
		case MessagesDefs::k_MESSAGE_LOGIN:
			if(connection != NULL)
			{
				for(int i = 0; i < Server::getConnections().size(); ++i)
				{
					if(Server::getConnections()[i]->getId() == id)
					{
						msg = new MessageDoubleLogIn(id);
						msg->isNeedCloseConnection = true;
						connection->addPendingMessage(msg);
						return;
					}
				}
				connection->setId(id);
				onLogIn(connection);
			}
			return;
		break;

		case MessagesDefs::k_MESSAGE_MOVE:
			msg = new MessageMove(id);
			msg->setData(data);
			ok = false;
		break;

		case MessagesDefs::k_MESSAGE_DO_ROCKET:
			msg = new MessageDoRocket(id);
		break;

		default:
			printf("Unknown message: id = %d\n", type);
			return;	
		break;
	}

	if(ok)
	{
		msg->setData(data);
	}

	for(int i = 0; i < Server::getConnections().size(); ++i)
	{
		Server::getConnections()[i]->addPendingMessage(msg);
	}
}

//====================================================================
//
//
//====================================================================
void MessagesHandler::onMove(Connection* connection, MessageMove* msg)
{
	if(msg->getId() == connection->getId())
	{
		connection->setPosX(msg->getPosX());
		connection->setPosY(msg->getPosY());
		connection->setRotation(msg->getRotation());
	}
}

//====================================================================
//
//
//====================================================================
void MessagesHandler::onLogIn(Connection* connection)
{
	MessageLogIn* msg = DataBaseConnection::getInstance()->getUserData(connection->getId());

	// To all
	if(msg != NULL)
	{
		for(int i = 0; i < Server::getConnections().size(); ++i)
		{
			Server::getConnections()[i]->addPendingMessage(msg);
		}
	}
	// To me
	for(int i = 0; i < Server::getConnections().size(); ++i)
	{
		msg = DataBaseConnection::getInstance()->getUserData(Server::getConnections()[i]->getId());

		if(msg != NULL)
		{
			connection->addPendingMessage(msg);
		}
	}
}

//====================================================================
//
//
//====================================================================
void MessagesHandler::addUnloginMsg(Connection* connection)
{
	Message* msg = new Message(MessagesDefs::k_MESSAGE_UNLOGIN, connection->getId());

	// To all
	if(msg != NULL)
	{
		for(int i = 0; i < Server::getConnections().size(); ++i)
		{
			Server::getConnections()[i]->addPendingMessage(msg);
		}
	}
}
